﻿using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsUpdateRepository : BaseRepository<MsUpdate>, IMsUpdateRepository
    {
        public async Task<MsUpdateDto> GetLastUpdate()
        {
            var list = await Db.MsUpdates
                .Where(o => !o.IsDeleted)
                .Select(r =>
                new MsUpdateDto()
                {
                    UpdateId = r.UpdateId,
                    FileName = r.FileName,
                    Version = r.Version
                }).AsNoTracking().ToListAsync().ConfigureAwait(false);
            return list.OrderByDescending(t => t.Version).Select(c => new MsUpdateDto()
            {
                UpdateId = c.UpdateId,
                FileName = c.FileName,
                Version = c.Version
            }).FirstOrDefault();
        }
    }
}